package com.autoglm.data.repository

import com.autoglm.data.local.dao.UserPreferenceDao
import com.autoglm.data.local.entity.UserPreferenceEntity
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import javax.inject.Inject
import javax.inject.Singleton

/**
 * 用户偏好仓库
 * 负责用户偏好设置的本地存储和管理
 */
@Singleton
class UserPreferenceRepository @Inject constructor(
    private val userPreferenceDao: UserPreferenceDao
) {
    
    /**
     * 获取所有偏好设置
     */
    fun getAllPreferences(): Flow<List<UserPreferenceEntity>> {
        return userPreferenceDao.getAllPreferences()
    }
    
    /**
     * 根据键获取偏好设置
     */
    suspend fun getPreferenceByKey(key: String): UserPreferenceEntity? {
        return userPreferenceDao.getPreferenceByKey(key)
    }
    
    /**
     * 根据键获取偏好设置（Flow）
     */
    fun getPreferenceByKeyFlow(key: String): Flow<UserPreferenceEntity?> {
        return userPreferenceDao.getPreferenceByKeyFlow(key)
    }
    
    /**
     * 根据类型获取偏好设置
     */
    fun getPreferencesByType(type: String): Flow<List<UserPreferenceEntity>> {
        return userPreferenceDao.getPreferencesByType(type)
    }
    
    /**
     * 设置字符串偏好
     */
    suspend fun setStringPreference(key: String, value: String) {
        val preference = UserPreferenceEntity(
            key = key,
            value = value,
            type = "string"
        )
        userPreferenceDao.insertPreference(preference)
    }
    
    /**
     * 设置布尔偏好
     */
    suspend fun setBooleanPreference(key: String, value: Boolean) {
        val preference = UserPreferenceEntity(
            key = key,
            value = value.toString(),
            type = "boolean"
        )
        userPreferenceDao.insertPreference(preference)
    }
    
    /**
     * 设置整数偏好
     */
    suspend fun setIntPreference(key: String, value: Int) {
        val preference = UserPreferenceEntity(
            key = key,
            value = value.toString(),
            type = "int"
        )
        userPreferenceDao.insertPreference(preference)
    }
    
    /**
     * 设置浮点数偏好
     */
    suspend fun setFloatPreference(key: String, value: Float) {
        val preference = UserPreferenceEntity(
            key = key,
            value = value.toString(),
            type = "float"
        )
        userPreferenceDao.insertPreference(preference)
    }
    
    /**
     * 获取字符串偏好
     */
    suspend fun getStringPreference(key: String, defaultValue: String = ""): String {
        return userPreferenceDao.getPreferenceByKey(key)?.value ?: defaultValue
    }
    
    /**
     * 获取布尔偏好
     */
    suspend fun getBooleanPreference(key: String, defaultValue: Boolean = false): Boolean {
        return userPreferenceDao.getPreferenceByKey(key)?.value?.toBoolean() ?: defaultValue
    }
    
    /**
     * 获取整数偏好
     */
    suspend fun getIntPreference(key: String, defaultValue: Int = 0): Int {
        return userPreferenceDao.getPreferenceByKey(key)?.value?.toIntOrNull() ?: defaultValue
    }
    
    /**
     * 获取浮点数偏好
     */
    suspend fun getFloatPreference(key: String, defaultValue: Float = 0f): Float {
        return userPreferenceDao.getPreferenceByKey(key)?.value?.toFloatOrNull() ?: defaultValue
    }
    
    /**
     * 删除偏好设置
     */
    suspend fun deletePreference(key: String) {
        userPreferenceDao.deletePreferenceByKey(key)
    }
    
    /**
     * 删除指定类型的所有偏好设置
     */
    suspend fun deletePreferencesByType(type: String) {
        userPreferenceDao.deletePreferencesByType(type)
    }
    
    /**
     * 删除所有偏好设置
     */
    suspend fun deleteAllPreferences() {
        userPreferenceDao.deleteAllPreferences()
    }
    
    /**
     * 检查偏好设置是否存在
     */
    suspend fun hasPreference(key: String): Boolean {
        return userPreferenceDao.getPreferenceByKey(key) != null
    }
}
